<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="bank" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体 -->
        <el-tree
          default-expand-all
          :data="data"
          :props="defaultProps"
        >
          <template slot-scope="{ node, data }">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px;width:100%;">
              <el-col :span="20">
                <svg-icon icon-class="bank" /><span>{{ node.label }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="isEdit ? '编辑部门' : '添加部门'"
    >
      <dept-dialog
        v-if="showDialog"
        :origin-list="originList"
        :pid="curId"
        :is-edit="isEdit"
        @success="hSuccess"
        @close="hClose"
      />
    </el-dialog>
  </div>
</template>
<script>
import deptDialog from './deptDialog.vue'
import { arrayToTree } from '@/utils/index'
import { getDepartment, delDepartment } from '@/api/department'
export default {
  components: { deptDialog },
  data() {
    return {
      originList: [],
      isEdit: false, // 记录当前是否处于编辑
      curId: '', // 当前选中的部门ID
      showDialog: false, // 是否显示弹层
      // 依赖一份树形数据
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created() {
    // 发送请求
    this.loadgetDepartment()
  },
  methods: {
    // 子组件抛出了关闭事件
    hClose() {
      // alert('子组件抛出了关闭事件')
      this.showDialog = false
    },
    hSuccess() {
      // 关闭弹层
      this.showDialog = false
      // 更新数据
      this.loadgetDepartment()
      // 关闭弹层
      this.showDialog = false
    },
    hAdd(id) {
      this.showDialog = true
      this.curId = id
    },
    hEdit(id) {
      // 更新当前id
      this.curId = id
      // 设置状态为编辑
      this.isEdit = true
      // 打开弹层
      this.showDialog = true
    },
    async doDel(id) {
      // 调用接口，做删除
      await delDepartment(id)
      // 提示一下
      this.$message.success('删除成功')
      // 更新数据
      this.loadgetDepartment()
    },
    hDel(id) {
      this.$confirm('此操作将永久删除部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 做删除
        this.doDel(id)
      }).catch(() => {
      })
    },
    async loadgetDepartment() {
      const res = await getDepartment()
      // 去掉第一项
      // 第一项是企业信息，不属于某个部门
      res.data.depts.shift()
      this.originList = res.data.depts.map(item => {
        return {
          id: item.id,
          name: item.name,
          code: item.code,
          pid: item.pid
        }
      })
      this.data = arrayToTree(res.data.depts)
    }
    // handleNodeClick(data) {
    //   console.log(data);
    // }
  }
}
</script>
